package com.test.demo;

/**
 * Created by jl on 2017/11/9.
 */
public class MyStack<T> {
    private static final int CAPACITY = 10; //default capacity 10
    private Object[] arr = new Object[CAPACITY];

    private int size;

    private void pushFirst(T t) {
        arr[0] = t;
        size++;
    }

    public void push(T t) {
        if (isEmpty()) {
            pushFirst(t);
            return;
        }
        if (size >= CAPACITY) {
            throw new RuntimeException("栈已满");
        }
        arr[size++] = t;
    }

    private boolean isEmpty() {
        return size == 0;
    }

    @SuppressWarnings("unchecked")
    public T pop() {
        Object obj = arr[size - 1];
        arr[--size] = null;
        return (T) obj;
    }

    public static void main(String[] args) {
        MyStack<String> stack = new MyStack<String>();
        stack.push("A");
        stack.push("B");
        System.out.println(stack.size);
        System.out.println(stack.pop());
        System.out.println(stack.size);
    }
}
